import { Ref, Component } from 'vue'

/**
 * 生成用于 defineExpose 将内部 Vue 组件 ref 暴露给外部的代理对象
 *
 * @param {Ref<Component>} ref Vue 组件 ref
 * @returns {Record<string, any>} 代理对象
 */
export const genExposeProxy = (ref: Ref<Component>): Record<string, any> => {
  return new Proxy(
    {},
    {
      get: (_, p: string) => (ref.value as Record<string, any>)?.[p],
      has: (_, p: string) => p in ref.value
    }
  )
}

export type GenExposeProxyType = typeof genExposeProxy
